﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace sistemaAcademicoFinanceiroEscolas.DynamicData.CustomPages.grupos
{
    public partial class alunosGrupo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Pegar a descricao da disciplina
            lblDescricaoGrupo.Text = Request["nomeGrupo"].ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();

            //deletar todos os professores da disciplina

            string sqlDeletar = "delete from alunoGrupo where codigoGrupo =" + Request["codigoGrupo"].ToString();
            SqlDataReader myReader = null;

            SqlCommand cmdDel = new SqlCommand(sqlDeletar, sqlc);

            myReader = cmdDel.ExecuteReader();
            cmdDel.Dispose();

            //cadastrar os professores que estavam marcados nos checkbox

            //loop em professores

            SqlConnection sqlc2 = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc2.Open();

            string sqlAlunos = "select * from aluno order by nomeAluno";

            SqlCommand cmdAluno = new SqlCommand(sqlAlunos, sqlc2);
            SqlDataReader myReaderAluno = cmdAluno.ExecuteReader();

            while (myReaderAluno.Read())
            {
                string valorRec = Request[myReaderAluno["codigoAluno"].ToString()];

                if (valorRec == "on")
                {

                    //inserir registro

                    SqlConnection sqlc3 = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                    sqlc3.Open();

                    string ins = "insert into alunoGrupo(codigoAluno, codigoGrupo) values (" + myReaderAluno["codigoAluno"].ToString() + "," + Request["codigoGrupo"].ToString() + ")";

                    SqlCommand cmdIns = new SqlCommand(ins, sqlc3);
                    SqlDataReader myReaderIns = cmdIns.ExecuteReader();

                    cmdIns.Dispose();

                    sqlc3.Close();

                }
            }

            sqlc.Close();
            sqlc2.Close();
            myReaderAluno.Close();


            Response.Redirect("alunosDefinidos.aspx");

        }
    }
}
